package com.shujia.death_lock;

public class Account {

    private int balance;

    public void transfer(Account target, int money) {
        synchronized (this) {
            System.out.println(Thread.currentThread().getName() + ":锁定转出账户成功");

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (target) {
                System.out.println(Thread.currentThread().getName() + ":锁定转入账户成功");

                balance -= money;// 扣钱
                target.balance += money;

                System.out.println(Thread.currentThread().getName() + ":转账成功");
            }
        }
    }


    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }
}
